package com.ease.integration.log;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * webservice日志拦截器 负责记录webservice接口上的参数
 * 
 * @author nathanleewei
 * 
 */
public class WebServiceLogger implements MethodInterceptor {

	private static final Log logger = LogFactory.getLog(WebServiceLogger.class);

	public Object invoke(MethodInvocation invocation) throws Throwable {
		String methodName = invocation.getMethod().getName();
		Object[] argv = invocation.getArguments();
		if (null != argv) {
			StringBuffer argsbuf = new StringBuffer();
			for (int i = 0; i < argv.length; i++) {
				Object arg = invocation.getArguments()[i];
				argsbuf.append("\targ[" + i + "]: " + arg + "\r\n");
			}
			if (logger.isDebugEnabled()) {
				logger.debug("methodName: " + methodName + "\r\nrequest:\r\n" + argsbuf.toString() + "\r\n\r\n");
			}
		}
		Object obj = null;
		try {
			obj = invocation.proceed();
		} catch (Exception e) {
			logger.error("web service invoke failed.", e);
		}
		if (logger.isDebugEnabled()) {
			logger.debug("methodName: " + methodName + "\r\nresponse:\r\n" + obj + "\r\n\r\n");
		}
		return obj;
	}

}
